package com.ruoyi.alarmmanagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.alarmmanagement.domain.DmWarn;
import com.ruoyi.alarmmanagement.entity.WXAlarm;
import com.ruoyi.alarmmanagement.entity.WXAlarmReqVO;
import com.ruoyi.alarmmanagement.mapper.DmWarnMapper;
import com.ruoyi.alarmmanagement.service.IDmWarnService;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * 告警Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-06
 */
@Service
public class DmWarnServiceImpl implements IDmWarnService
{
    @Autowired
    private DmWarnMapper dmWarnMapper;

    /**
     * 查询告警
     *
     * @param ID 告警主键
     * @return 告警
     */
    @Override
    public DmWarn selectDmWarnByID(Long ID)
    {
        return dmWarnMapper.selectDmWarnByID(ID);
    }

    /**
     * 查询告警列表
     *
     * @param dmWarn 告警
     * @return 告警
     */
    @Override
    public List<DmWarn> selectDmWarnListXb(DmWarn dmWarn)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (!Objects.equals(user.getSystemFlag(), Constants.SYS_FLAG_ADMIN)) {
            Long userId = user.getUserId();
            dmWarn.setSysUserId(userId);
        }
        if (Objects.equals(user.getSystemFlag(), Constants.SYS_FLAG_NORMAL_USER)) {
            dmWarn.setTenantId(user.getTenantId());
        }
        if (dmWarn.getBeginStartTime() == null || dmWarn.getEndStartTime() == null){
            dmWarn.setBeginStartTime(LocalDate.now().minusDays(1).toString());
            dmWarn.setEndStartTime(LocalDate.now().plusDays(1).toString());
        } else if (StringUtils.isNotBlank(dmWarn.getEndStartTime())) {
            dmWarn.setEndStartTime(LocalDate.parse(dmWarn.getEndStartTime()).plusDays(1).toString());
        }
        return dmWarnMapper.selectDmWarnListXb(dmWarn);
    }


    @Override
    public List<DmWarn> selectDmWarnListDb(DmWarn dmWarn)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (!Objects.equals(user.getSystemFlag(), Constants.SYS_FLAG_ADMIN)) {
            Long userId = user.getUserId();
            dmWarn.setSysUserId(userId);
        }
        if (Objects.equals(user.getSystemFlag(), Constants.SYS_FLAG_NORMAL_USER)) {
            dmWarn.setTenantId(user.getTenantId());
        }
        if (StringUtils.isAnyBlank(dmWarn.getBeginStartTime(), dmWarn.getEndStartTime())){
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            dmWarn.setBeginStartTime(LocalDate.now().minusDays(1).toString());
            dmWarn.setEndStartTime(LocalDate.now().plusDays(1).toString());
        } else if (StringUtils.isNotBlank(dmWarn.getEndStartTime())) {
            dmWarn.setEndStartTime(LocalDate.parse(dmWarn.getEndStartTime()).plusDays(1).toString());
        }
        return dmWarnMapper.selectDmWarnListDb(dmWarn);
    }

    /**
     * 新增告警
     *
     * @param dmWarn 告警
     * @return 结果
     */
    @Override
    public int insertDmWarn(DmWarn dmWarn)
    {
        return dmWarnMapper.insertDmWarn(dmWarn);
    }

    /**
     * 修改告警
     *
     * @param dmWarn 告警
     * @return 结果
     */
    @Override
    public int updateDmWarn(DmWarn dmWarn)
    {
        return dmWarnMapper.updateDmWarn(dmWarn);
    }

    /**
     * 批量删除告警
     *
     * @param IDs 需要删除的告警主键
     * @return 结果
     */
    @Override
    public int deleteDmWarnByIDs(Long[] IDs)
    {
        return dmWarnMapper.deleteDmWarnByIDs(IDs);
    }

    /**
     * 删除告警信息
     *
     * @param ID 告警主键
     * @return 结果
     */
    @Override
    public int deleteDmWarnByID(Long ID)
    {
        return dmWarnMapper.deleteDmWarnByID(ID);
    }

    @Override
    public Map<String,Integer> GetAlarmStatisticsData() {
        SimpleDateFormat foramt = new SimpleDateFormat("yyyy-MM-dd");
        Date nowTime = new Date();
        //压力预警
        Long press = dmWarnMapper.selectCount(new LambdaQueryWrapper<DmWarn>()
                .eq(DmWarn::getWarnType, 21)
                .eq(DmWarn::getTerminalType,2)
                .between(DmWarn::getRecentlyWarnTime, DateUtil.getLastDayStartDate(foramt.format(nowTime)), foramt.format(nowTime)+" 23:59:59"));
        //日用量预警
        Long dosage = dmWarnMapper.selectCount(new LambdaQueryWrapper<DmWarn>()
                .eq(DmWarn::getWarnType, 22)
                .eq(DmWarn::getTerminalType,2)
                .between(DmWarn::getRecentlyWarnTime, DateUtil.getLastDayStartDate(foramt.format(nowTime)), foramt.format(nowTime)+" 23:59:59"));
        //流量预警
        Long flow = dmWarnMapper.selectCount(new LambdaQueryWrapper<DmWarn>()
                .eq(DmWarn::getWarnType, 20)
                .eq(DmWarn::getTerminalType,2)
                .between(DmWarn::getRecentlyWarnTime, DateUtil.getLastDayStartDate(foramt.format(nowTime)), foramt.format(nowTime)+" 23:59:59"));
        Map<String,Integer> map = new HashMap<>();
        map.put("PressureAlarmCount",press.intValue());
        map.put("GPSAlarmCount",dosage.intValue());
        map.put("FlowUnusualAlarmCount",flow.intValue());
        return map;

    }

    @Override
    public List<WXAlarm> GetAlarmList(WXAlarmReqVO alarmReqVO) {
        return dmWarnMapper.GetAlarmList(alarmReqVO);
//        DmWarn dmWarn = new DmWarn();
//        dmWarn.setWarnType(Integer.parseInt(alarmTypeId));
//        dmWarn.setDescription(description);
//        dmWarn.getParams().put("beginStartTime",startDate);
//        dmWarn.getParams().put("endStartTime",endDate);
//
//        return dmWarnMapper.selectDmWarnListDb(dmWarn);
    }
}
